package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SystemMessage {
  private static Logger _log = Logger.getLogger(SystemMessage.class.getName());
  
  private static SystemMessage _instance;
  
  private final HashMap<Integer, L1WilliamSystemMessage> _itemIdIndex;
  
  public static SystemMessage getInstance() {
    if (_instance == null)
      _instance = new SystemMessage(); 
    return _instance;
  }
  
  private SystemMessage() {
    this._itemIdIndex = new HashMap<>();
    loadSystemMessage();
  }
  
  private void loadSystemMessage() {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM william_Message");
      rs = pstm.executeQuery();
      fillSystemMessage(rs);
    } catch (SQLException e) {
      _log.log(Level.SEVERE, "error while creating william_Message table", e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  private void fillSystemMessage(ResultSet rs) throws SQLException {
    while (rs.next()) {
      int Id = rs.getInt("id");
      String Message = rs.getString("message");
      L1WilliamSystemMessage System_Message = new L1WilliamSystemMessage(Id, Message);
      this._itemIdIndex.put(Integer.valueOf(Id), System_Message);
    } 
  }
  
  public L1WilliamSystemMessage getTemplate(int Id) {
    return this._itemIdIndex.get(Integer.valueOf(Id));
  }
}
